package ecprac.drivers.def;

import ecprac.ea.abstracts.AbstractDriver;
import ecprac.ea.abstracts.map.Corner;
import ecprac.torcs.client.Action;
import ecprac.torcs.client.SensorModel;
import ecprac.torcs.genome.IGenome;

public class DefaultDriver extends AbstractDriver {

	private double c0 = 0.5;
	private double c1 = 0.5;
	
	private String driverName;
	
	DefaultDriver (String name) {
		driverName = name;
	}
	
	public void loadGenome(IGenome genome) {

		if (genome instanceof DefaultDriverGenome) {
			DefaultDriverGenome llgenome = (DefaultDriverGenome) genome;
			
			c0 = llgenome.c0;
			c1 = llgenome.c1;


		} else {
			System.err.println("Invalid Genome assigned");
		}

	}

	/**
	 * Returns a number. 1 = accelerate, -1 = decelerate.
	 */
	public double getAcceleration(SensorModel sensors){
		
		Corner c = trackmap.getNextCorner(sensors.getDistanceFromStartLine());
		if(c.distance > 50){
			// Accelerate if needed
			if(sensors.getSpeed() < 200 * c0){
				return 1;
			} else {
				return 0;
			}
		} else {
			if(sensors.getSpeed() > 100 * c0){
				// Brake
				return -1;
			} else {
				// Continue
				return 1;
			}
		}
		
		
	}
	
	public double getSteering(SensorModel sensors){
		Corner c = trackmap.getNextCorner(sensors.getDistanceFromStartLine());
		// Data collection part. This is done by recording the Corner.length, Corner.sharpness, Sensor.TrackPos, Sensor.Angle and storing this in a CSV file.
		// This is recorded at the beginning of a corner.
		double sharpness;
		double length;
		double trackPos;
		double angle;
		if (Math.abs(sensors.getDistanceFromStartLine() - (c.location - c.length/2.0)) < 0.5 ) {
			System.out.println("Driver: " +driverName +"\tDistance: "+Math.abs(sensors.getDistanceFromStartLine() - (c.location - c.length/2.0)));
			sharpness = c.sharpness;
			length = c.length;
			trackPos = sensors.getTrackPosition();
			angle = sensors.getAngleToTrackAxis();
			DefaultEA.records.add(new Record(sharpness, length, trackPos, angle));
		}
		
		

		if(c.distance > 0){
			// You are approaching a corner
			if(c.sharpness < 0){
				//Corner to the left
				if( sensors.getTrackPosition() < c1 ){
					// Move to the right side of the track
					return utils.moveTowardsTrackPosition( sensors, Math.abs(sensors.getTrackPosition() - c1), c1 );	
				} else {
					// Straight on
					return utils.alignToTrackAxis(sensors, Math.abs(sensors.getAngleToTrackAxis()));
				}
			} else {
				//Corner to the right
				if( sensors.getTrackPosition() > -1 * c1 ){
					// Move to the left side of the track
					return utils.moveTowardsTrackPosition( sensors, Math.abs(sensors.getTrackPosition() - c1), -1 * c1 );	
				} else {
					// Straight on
					return utils.alignToTrackAxis(sensors, Math.abs(sensors.getAngleToTrackAxis()));
				}
			}
		} else {
			// You are in a corner, so move to the track axis
			return utils.moveTowardsTrackPosition(sensors, 2*Math.abs(sensors.getTrackPosition()), 0);
		}
		
	}
	
	
	public String getDriverName() {
		return "Car " +driverName;
	}

	
	/*
	 * The following methods are only here as a reminder that you can,
	 * and may change all driver methods, including the already implemented
	 * ones, such as those beneath.
	 */
	public void controlQualification(Action action, SensorModel sensors) {	
		super.controlQualification(action, sensors);
	}
	
	public void controlRace(Action action, SensorModel sensors) {
		super.controlRace(action, sensors);
	}
	
	public void defaultControl(Action action, SensorModel sensors){
		super.defaultControl(action, sensors);
	}
	
	
}
